#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020-present Fewtarius

###
### Simple script to build AmberELEC
###

if [ !"${ARCH}" == true ]
then
  echo "export ARCH before building."
  exit 1
fi

export DISTRO=AmberELEC
export PROJECT=Rockchip

echo "Building ${DISTRO} for ${DEVICE}"
if [ ! -d "target" ]
then
  mkdir target
fi

# Clean a few packages to ensure the build date and version are updated
for package in amberelec emulationstation retroarch retrorun lib32
do
  ./scripts/clean ${package}
done

if [ "${ARCH}" == "aarch64" ]
then
  make image
elif [ "${ARCH}" == "arm" ]
then
  # Only build the 32bit packages that we need rather than the whole image
  # to speed up the build process and save disk space.
  for package in build-lib32
  do
    scripts/build ${package}
    if [ ! $? == 0 ]
    then
      echo "Build failed..exiting."
      exit 1
    fi
  
    scripts/install ${package}
    if [ ! $? == 0 ]
    then
      echo "Build failed..exiting."
      exit 1
    fi
  done
fi

if [ ! $? == 0 ]
then
  echo "Build failed..exiting."
  exit 1
fi

if [ "${ARCH}" == "aarch64" ]
then
  if [ -d "release/${ARCH}/${DEVICE}" ]
  then
    rm -rf "release/${ARCH}/${DEVICE}"
  fi
mkdir -p release/${ARCH}/${DEVICE}
mv target/* "release/${ARCH}/${DEVICE}/"
fi
